package com.itlong.cloud.controller.system;

import com.itlong.cloud.POJO.DTO.operate.OperateAssessFunctionDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateAssessFunctionVO;
import com.itlong.cloud.POJO.VO.operate.OperateGetRoleMenuVO;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.noncore.operate.IOperateAssessFunctionServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      报价系统菜单资源控制器
 * </desc>
 *
 * @createDate2020/6/8
 **/
@RestController
@RequestMapping("/system/assessFunction")
public class OperateAssessFunctionController {

    @Autowired
    private IOperateAssessFunctionServiceClient iOperateAssessFunctionServiceClient;

    /**
     * <desc>
     *      分页获取报价系统菜单资源
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/8
     */
    @RequestMapping(value = "getPage",method = RequestMethod.POST)
    public Object getPage(OperateAssessFunctionDTO operateAssessFunctionDTO, HttpServletRequest request){
        try{
            operateAssessFunctionDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssessFunctionDTO.class,request);
            if (operateAssessFunctionDTO == null || StringUtils.isBlank(operateAssessFunctionDTO.getFunctionId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<OperateAssessFunctionVO> page = iOperateAssessFunctionServiceClient.getPage(operateAssessFunctionDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        }catch (Exception e){
            throw new DataAccessException("【运营_报价系统菜单管理getPage】分页获取报价系统菜单失败",e);
        }
    }

    /**
     * <desc>
     *      获取单个菜单信息
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/8
     */
    @RequestMapping(value = "getOne",method = RequestMethod.POST)
    public Object getOne(OperateAssessFunctionDTO operateAssessFunctionDTO, HttpServletRequest request){
        try {
            operateAssessFunctionDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssessFunctionDTO.class,request);
            if(operateAssessFunctionDTO == null || StringUtils.isBlank(operateAssessFunctionDTO.getFunctionId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            OperateAssessFunctionVO operateAssessFunctionVO = iOperateAssessFunctionServiceClient.getOne(operateAssessFunctionDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),operateAssessFunctionVO);
        }catch (Exception e){
            throw new DataAccessException("【运营_报价系统菜单管理getOne】获取单个报价系统菜单失败",e);
        }
    }

    /**
     * <desc>
     *      更新报价系统菜单
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/8
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public Object update(OperateAssessFunctionDTO operateAssessFunctionDTO, HttpServletRequest request){
        try {
            operateAssessFunctionDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssessFunctionDTO.class, request);
            String myUserName = request.getAttribute("myUserName") == null ? null : request.getAttribute("myUserName").toString();

            if (operateAssessFunctionDTO == null || StringUtils.isBlank(myUserName) || StringUtils.isBlank(operateAssessFunctionDTO.getFunctionId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iOperateAssessFunctionServiceClient.update(operateAssessFunctionDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_报价系统菜单管理update】更新报价系统菜单失败",e);
        }
    }

    /**
     * <desc>
     *      保存报价系统菜单
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/8
     */
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public Object save(OperateAssessFunctionDTO operateAssessFunctionDTO, HttpServletRequest request){
        try {
            operateAssessFunctionDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssessFunctionDTO.class,request);
            String myUserName = request.getAttribute("myUserName") == null ? null : request.getAttribute("myUserName").toString();
            operateAssessFunctionDTO.setLoginUserId(myUserName);
            if (operateAssessFunctionDTO == null || StringUtils.isBlank(operateAssessFunctionDTO.getFunctionName())
                    ||operateAssessFunctionDTO.getOrders() == null || operateAssessFunctionDTO.getFunctionType() == null){
                return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(operateAssessFunctionDTO.getFunctionType(),1,2)){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            if (operateAssessFunctionDTO.getIdentifier() == null) {
                operateAssessFunctionDTO.setIdentifier("");
            }
            iOperateAssessFunctionServiceClient.save(operateAssessFunctionDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_报价系统菜单管理save】保存报价系统菜单失败",e);
        }
    }

    /**
     * <desc>
     *      删除报价系统菜单
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/8
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public Object delete(OperateAssessFunctionDTO operateAssessFunctionDTO, HttpServletRequest request){
        try {
            operateAssessFunctionDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssessFunctionDTO.class,request);
            String myUserName = request.getAttribute("myUserName") == null ? null : request.getAttribute("myUserName").toString();
            if (operateAssessFunctionDTO == null || StringUtils.isBlank(operateAssessFunctionDTO.getFunctionIds()) || StringUtils.isBlank(myUserName)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            operateAssessFunctionDTO.setLoginUserId(myUserName);
            iOperateAssessFunctionServiceClient.delete(operateAssessFunctionDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_报价系统菜单管理delete】删除报价系统菜单失败",e);
        }
    }

    /**
     * <desc>
     *      获取当前节点子节点
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/8
     */
    @RequestMapping(value = "getChilds",method = RequestMethod.POST)
    public Object getChild(OperateAssessFunctionDTO operateAssessFunctionDTO, HttpServletRequest request){
        try {
            operateAssessFunctionDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssessFunctionDTO.class,request);
            if (operateAssessFunctionDTO == null || StringUtils.isBlank(operateAssessFunctionDTO.getFunctionId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<OperateAssessFunctionVO> operateAssessFunctionVOS =  iOperateAssessFunctionServiceClient.getChild(operateAssessFunctionDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), operateAssessFunctionVOS);
        }catch (Exception e){
            throw new DataAccessException("【运营_报价系统菜单管理getChild】获取报价系统菜单子节点失败",e);
        }
    }

    /**
     * <desc>
     *      启用或禁用
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/8
     */
    @RequestMapping(value = "banOrUse",method = RequestMethod.POST)
    public Object banOrUse(OperateAssessFunctionDTO operateAssessFunctionDTO, HttpServletRequest request){
        try {
            operateAssessFunctionDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssessFunctionDTO.class, request);
            String myUserName = request.getAttribute("myUserName") == null ? null : request.getAttribute("myUserName").toString();
            operateAssessFunctionDTO.setLoginUserId(myUserName);
            if (operateAssessFunctionDTO == null || StringUtils.isBlank(myUserName) || StringUtils.isBlank(operateAssessFunctionDTO.getFunctionId())||operateAssessFunctionDTO.getStatus() == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(operateAssessFunctionDTO.getStatus(),1,2)){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            iOperateAssessFunctionServiceClient.updateStatus(operateAssessFunctionDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_报价系统菜单管理banOrUse】启用或禁用报价系统菜单子节点失败",e);
        }
    }

    /**
     * <desc>
     *      获取父节点
     * </desc>
     *
     * @param operateAssessFunctionDTO
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/16
     */
    @RequestMapping("/getParents")
    public Object getParentsLevelFunctions(OperateAssessFunctionDTO operateAssessFunctionDTO, HttpServletRequest request){
        try {
            operateAssessFunctionDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssessFunctionDTO.class, request);
            if (operateAssessFunctionDTO == null || StringUtils.isBlank(operateAssessFunctionDTO.getParent())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<Map<String, Object>> resultList = iOperateAssessFunctionServiceClient.getParentLevelFunctions(operateAssessFunctionDTO.getParent());
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), resultList);
        }catch (Exception e){
            throw new DataAccessException("【运营_报价系统菜单管理getParents】获取父节点失败",e);
        }
    }

}
